package com.example.administrator.im.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.im.MyApplication;
import com.example.administrator.im.R;
import com.example.administrator.im.config.ImageConfig;
import com.example.administrator.im.data.LinkmanListData;
import com.example.administrator.im.utils.DownloadImageUtils;
import com.example.administrator.im.utils.FileUtils;
import com.example.administrator.im.utils.HttpUtils;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * create:2018/9/22 14:16
 * author: 姚鸿杰
 **/
public class Linkman_List_Adapter extends BaseExpandableListAdapter {
    private final String TAG = "Linkman_List_Adapter";

    public Linkman_List_Adapter(String[] groups, LinkmanListData[][] childs, Context context) {
        if (groups != null && childs != null) {
            this.groups = groups;
            this.childs = childs;
        }
        this.mContext = context;
    }

    private String[] groups;
    private LinkmanListData[][] childs;
    private Context mContext;

    public void addGroup(String[] groups) {
        this.groups = groups;
        notifyDataSetChanged();
    }

    public void addChildren(LinkmanListData[][] childs) {
        this.childs = childs;
        notifyDataSetChanged();
    }

    public void setData(String[] groups, LinkmanListData[][] childs) {
        this.groups = groups;
        this.childs = childs;
        notifyDataSetChanged();
    }

    /**
     * 一级列表个数
     *
     * @return ：一级列表个数
     */
    @Override
    public int getGroupCount() {
        return groups.length;
    }

    /**
     * 每个二级列表个数
     *
     * @param groupPosition ： 选中的position
     * @return ：每个二级列表个数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return childs[groupPosition].length;
    }

    /**
     * 一级列表单个item
     *
     * @param position ：第几个
     * @return ：返回一级列表的第几个item
     */
    @Override
    public Object getGroup(int position) {
        return groups[position];
    }

    /**
     * 二级列表单个item
     *
     * @param gPosition ：一级列表position
     * @param cPosition ：二级列表postion
     * @return ：返回二级列表单个item
     */
    @Override
    public Object getChild(int gPosition, int cPosition) {
        return childs[gPosition][cPosition];
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    /**
     * 每个item的id是否是固定的，一般设置为true
     *
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }


    /**
     * 填充一级列表
     *
     * @param position
     * @param isExpanded  ：是否已展开
     * @param convertView
     * @param viewGroup
     * @return
     */
    @Override
    public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup viewGroup) {

        if (convertView == null) {
            convertView = LayoutInflater.from(MyApplication.getmContext()).inflate(R.layout.list_one_linkman, null);
        }
        TextView tv_group = (TextView) convertView.findViewById(R.id.list_one_title);
        tv_group.setText(groups[position]);
        return convertView;
    }

    /**
     * 填充二级列表
     *
     * @param gPosition
     * @param cPosition
     * @param isLastChild
     * @param convertView
     * @param viewGroup
     * @return
     */
    @Override
    public View getChildView(int gPosition, int cPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(MyApplication.getmContext()).inflate(R.layout.list_two_linkman, null);
        }
        TextView tv_child = (TextView) convertView.findViewById(R.id.list_two_title);
        CircleImageView img = (CircleImageView) convertView.findViewById(R.id.friend_img);

        LinkmanListData data = childs[gPosition][cPosition];
        tv_child.setText(data.getNickname());
        String url = data.getUrl();

        if (!TextUtils.equals(url, HttpUtils.PORT)) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.login_logo)
                    .error(R.mipmap.login_logo)
                    .override(ImageConfig.getItemSmall(), ImageConfig.getItemSmall());
            Glide.with(mContext).load(url).apply(options).into(img);

            File mFile = FileUtils.getFilePath(data.getUsername(), FileUtils.FRIEND_PACK);
            saveImage(url, mFile, ImageConfig.getItemSmall());
        }
        return convertView;
    }


    private void saveImage(String url, final File file, int size) {
        if (!file.exists()) {
            DownloadImageUtils downloadImage = new DownloadImageUtils(url, MyApplication.getmContext(),
                    size, size, file, new DownloadImageUtils.ImagedownLoadCallBack() {
                @Override
                public void onDownLoadSuccess(final File file) {
                }

                @Override
                public void onDownLoadFailed() {
                }
            });
            new Thread(downloadImage).start();
        }
    }

    /**
     * 级列表中每个能否被选中，如果有点击事件一定要设为true
     *
     * @param gPosition
     * @param cPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int gPosition, int cPosition) {
        return true;
    }
}